package com.hmdp.controller;


import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.service.IShopTypeService;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/shop-type")
public class ShopTypeController {
    @Resource
    private IShopTypeService typeService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("list")
    public Result<List> queryTypeList() {
        String key = RedisConstants.SHOP_TYPE_KEY;
        List list = redisTemplate.opsForList().range(key, 0,-1);
        if (!list.isEmpty()) {
            return Result.ok(list);
        }
        List<ShopType> typeList = typeService
                .query().orderByAsc("sort").list();
        redisTemplate.opsForList().leftPushAll(key,typeList);
        redisTemplate.expire(key,1L, TimeUnit.HOURS);
        return Result.ok(typeList);
    }
}
